package leetcode.code2162;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	int moveCost;

	public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
		this.moveCost = moveCost;
		List<String> ways = ways(targetSeconds);
		int ans = Integer.MAX_VALUE;
		for (int i = 0; i < ways.size(); i++) {
			char[] cs = ways.get(i).toCharArray();
			int from = startAt;
			int cost = 0;
			for (int j = 0; j < cs.length; j++) {
				int to = cs[j] - '0';
				cost += this.move(from, to);
				cost += pushCost;
				from = to;
			}
			ans = Math.min(ans, cost);
		}
		return ans;
	}

	private List<String> ways(int targetSeconds) {
		List<String> ans = new ArrayList<>();
		int div = targetSeconds / 60;
		int mod = targetSeconds % 60;
		if (div < 100) {// 注意：小于100
			ans.add(this.minutes(div) + this.seconds(mod, div));
		}
		if (div > 0 && mod < 40) {// 注意：小于40
			ans.add(this.minutes(div - 1) + this.seconds(mod + 60, div - 1));
		}
		return ans;
	}

	public String minutes(int minute) {
		return minute == 0 ? "" : String.valueOf(minute);
	}

	public String seconds(int seconds, int minute) {
		if (minute != 0) {
			return seconds == 0 ? "00" : seconds < 10 ? "0" + seconds : String.valueOf(seconds);
		} else {
			return String.valueOf(seconds);// 注意：没有分钟
		}
	}

	public int move(int from, int to) {
		return from == to ? 0 : moveCost;
	}

	@Override
	public void debug4() {
		H.compare(65817, this.minCostSetTime(1, 9403, 9402, 6008));
		H.compare(26156, this.minCostSetTime(5, 3269, 3270, 650));
		H.compare(5, this.minCostSetTime(0, 1, 4, 9));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
